package dataStructure;

import java.lang.reflect.Field;

public class Person {
    String name;
    int age;
    Person p;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Person getP() {
        return p;
    }

    public void setP(Person p) {
        this.p = p;
    }

    public Person(){

    }
    public Person(String name, int age, Person p) {
        this.name = name;
        this.age = age;
        this.p = p;
    }
    public static  void travese(Person person){
        // 遍历输出属性
        Field[] fields =  person.getClass().getDeclaredFields();
        for( int i = 0; i < fields.length; i++){
            Field f = fields[i];
            try {
                if (f.get(person) instanceof Person){
                    travese((Person) f.get(person));
                }else{
                    System.out.println("属性名："+f.getName()+",属性值："+f.get(person));
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", p=" + p +
                '}';
    }

    public static void main(String[] args) {
        Person personPar = new Person();
        personPar.setAge(20);
        personPar.setName("wf");
        Person personzi = new Person();
        personzi.setAge(21);
        personzi.setName("wf2");
        Person personsun = new Person();
        personsun.setAge(22);
        personsun.setName("wf3");

        personzi.setP(personsun);
        personPar.setP(personzi);

        System.out.println(personPar);
        travese(personPar);
    }
}
